package com.wuchengdong.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wuchengdong.web.mapper.AppointmentMapper;
import com.wuchengdong.web.pojo.Appointment;
import com.wuchengdong.web.pojo.Order;
import com.wuchengdong.web.pojo.Payment;
import com.wuchengdong.web.pojo.dto.AppointmentDTO;
import com.wuchengdong.web.service.AppointmentService;
import com.wuchengdong.web.service.OrderService;
import com.wuchengdong.web.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
public class AppointmentServiceImpl extends ServiceImpl<AppointmentMapper, Appointment> implements AppointmentService {
    @Autowired
    private OrderService orderService;
    @Autowired
    private PaymentService paymentService;

    @Override
    @Transactional
    public Order
    createNewAppointment(AppointmentDTO appointmentDTO) {
        Appointment appointment = new Appointment()
                .setUserId(appointmentDTO.getUserId())
                .setServiceId(appointmentDTO.getServiceId())
                .setEmployeeId(appointmentDTO.getEmployeeId())
                .setAppointmentTime(appointmentDTO.getAppointmentTime())
                .setStatus(appointmentDTO.getAppointmentStatus());
        if(!checkTime(appointment)) {
            return null;
        }
        this.save(appointment);
        Order order = new Order()
                .setAppointmentId(appointment.getId())
                .setUserId(appointmentDTO.getUserId())
                .setTotalPrice(appointmentDTO.getTotalPrice())
                .setPaymentStatus(appointmentDTO.getOrderStatus());
        orderService.save(order);
        return order;
    }

    /**
     * 检查预约时间段该名员工是否有预约
     */
    private boolean checkTime(Appointment appointment) {
        Integer employeeId = appointment.getEmployeeId();
        if(employeeId != null) {
            return this.list(
                    new LambdaQueryWrapper<Appointment>()
                            .eq(Appointment::getEmployeeId, employeeId)
                            .between(Appointment::getAppointmentTime, appointment.getAppointmentTime(), appointment.getAppointmentTime().plusHours(1))
            ).isEmpty();
        }else {
            return true;
        }
    }

    @Override
    @Transactional
    public boolean updateStatus(Appointment appointment) {
        this.updateById(appointment);
        if (appointment.getStatus().equals("completed")){
            Order order = orderService.getOne(
                    new LambdaQueryWrapper<Order>()
                            .eq(Order::getAppointmentId, appointment.getId())
            );
            List<Payment> paymentList = paymentService.list(
                    new LambdaQueryWrapper<Payment>()
                            .eq(Payment::getOrderId, order.getId())
            );
            BigDecimal totalPrice = order.getTotalPrice();
            for (Payment payment : paymentList) {
                totalPrice = totalPrice.subtract(payment.getAmount());
            }
            // 创建payment表 客户端可以付款
            paymentService.save(
                    new Payment()
                            .setOrderId(order.getId())
                            .setPaymentMethod("other")
                            .setAmount(totalPrice)
                            .setTransactionId("abcdefghijklmn")
                            .setStatus("pending")
            );
        }else if (appointment.getStatus().equals("canceled")){
            Order order = orderService.getOne(
                    new LambdaQueryWrapper<Order>()
                            .eq(Order::getAppointmentId, appointment.getId())
            );
            orderService.updateById(order.setPaymentStatus("canceled"));
        }
        return true;
    }
}
